<?php
ini_set("display_errors",1);
error_reporting(E_ALL);
$posted = &$_POST;
$ip= $posted["IP"];
$id= $posted["ID"];
$name= $posted["Name"];
if($posted['action']=="Initiate connection") {
	//To keep track of running task.
	$fname="frequency.txt";
	$nfile = fopen($fname, "w");
	if($nfile != false) { 
		fwrite($nfile,"1");
		fclose($nfile);
	};
	//Run the damn thing.
	$WshShell = new COM("WScript.Shell");
	//$oExec = $WshShell->Run("./php/php-win.exe backgroundProcess.php {$ip},{$id},{$name}", 0, false);
	$oExec = $WshShell->Run("php\php-win.exe -f backgroundProcess.php", 0, false);
	//This is so we can allow the background process to properly close before running a new one.  Or to at least detect a broken socket.
	sleep(5); 
	$fname="debugger.txt";
	$nfile = fopen($fname, "w");
    $socketTest=fread($nfile,100);
    fclose($nfile);
    if(strpos($socketTest,'Can not open socket') !== false) {
        print("Socket can not be opened.  99% chance it's some permissions or firewall issue.");
    } else{
        print("Bridge has made contact (it's only a tiny bit shorter than the Vasco da Gama bridge).  You may close out of this application now.  Data from controller should appear in Connect within the next 5 minutes.  Time is stored in UTC format.");
    };
	$fname="debugger.txt";
	$nfile = fopen($fname, "w");
	if($nfile != false) { 
		fwrite($nfile,"Foreground process success.");
		fclose($nfile);
	};
};
print("<BR><a href='index.php'>Go back</a>");
?>
