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];
		$port = 80;
	$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";
		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\

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

		#socket_set_blocking ($conn, false);
			echo $response .= fgets($conn, 2048);
			if(strpos($response, "</params>" ))
	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.