Socket programming

Dear Gurus,

i have a program in php that uses php to send fsockopen to connect to a site. when i run the program on the terminal, it works fine but when i run it via the browser, connection to failed. what could be the problem

function XML_request($site, $location, $data, $agent,$auth){
	$response = "";
	$site = explode(':', $site);
	if(isset($site[1]) and is_numeric($site[1])){
		$port = $site[1];
	}else{
		$port = 80;
	}
	$return='';
	$site = $site[0];

	$conn = fsockopen ($site, $port); #open the connection
	if(!$conn){ #if the connection was not opened successfully
		$response="Connection failed to $site : $port";
	}else{
		echo "Connected to $site : $port";
		echo $headers =
			"POST $location HTTP/1.1\\r\
" .
			"Host: $site\\r\
" .
			"Content-Type: text/xml\\r\
" .
			"User-Agent: ". $agent . "\\r\
" .
			"Content-Length: " . strlen($data) . "\\r\
" .
			"Authorization: Basic ". $auth . "\\r\
\\r\
";
			

		fputs($conn, "$headers");
		fputs($conn, $data);

		#socket_set_blocking ($conn, false);
		while(!feof($conn)){
			echo $response .= fgets($conn, 2048);
			if(strpos($response, "</params>" ))
			break;
		}
		$response.="\\r\
</methodResponse>";
		fclose($conn);
	}
	return $response;
}

Difficult to tell without seeing it in action. Scripts which run via the browser can have unexpected behaviour when run as CLI and vice-versa.

Your best bet is to start logging things to a database.