Some people say that when writing to a socket not all of the bytes requested to be written may be written. You may have to call fwrite again to write bytes that were not written the first time. (At least this is how the write() system call in UNIX works.)
This is helpful code (warning: not tested with multi-byte character sets)
function fwrite_with_retry($sock, &$data)
{
$bytes_to_write = strlen($data);
$bytes_written = 0;
while ( $bytes_written < $bytes_to_write )
{
if ( $bytes_written == 0 ) {
$rv = fwrite($sock, $data);
} else {
$rv = fwrite($sock, substr($data, $bytes_written));
}
if ( $rv === false || $rv == 0 )
return( $bytes_written == 0 ? false : $bytes_written );
$bytes_written += $rv;
}
return $bytes_written;
}
Call this like so:
$rv = fwrite_with_retry($sock, $request_string);
if ( ! $rv )
die("unable to write request_string to socket");
if ( $rv != strlen($request_string) )
die("sort write to socket on writing request_string");