All I can say is that the code above works on my mac. Just typing
$ php test.php
with test.php the content of the above works perfectly.
This is the version on my mac:
$ php --version
PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
I also retested it on my webservers ... (OpenBSD, php compiled from source code) ... it just works.
It just times out with an error of 0.
That's on the connect (fsockopen() ) I presume ? - not on the fread ?
The fread goes hanging if you start to read to soon (e.g. before sending the \r\n on the request ...), but if you used the code verbatim ...
That would make no sense unless there's something in your php configuration or so that's making that it isn't sending or receiving the SYN or S/A packets (cause that's all that's needed to get through this).
I'd check what your php code is actually sending out on the wire.
replace XXX with the right interface name
# tcpdump -n -i XXX port 43
This is what I get where it works:
10:13:39.705217 193.0.6.135.43 > MYIP.20252: S 250152799:250152799(0) ack 3119891155 win 14480 <mss 1460,sackOK,timestamp 496755460 4214646201,nop,wscale 7> (DF)
10:13:39.705241 MYIP.20252 > 193.0.6.135.43: . ack 1 win 16384 <nop,nop,timestamp 4214646201 496755460> (DF)
10:13:39.705315 MYIP.20252 > 193.0.6.135.43: P 1:11(10) ack 1 win 16384 <nop,nop,timestamp 4214646201 496755460> (DF)
10:13:39.710924 193.0.6.135.43 > MYIP.20252: . ack 11 win 114 <nop,nop,timestamp 496755466 4214646201> (DF)
10:13:39.711172 193.0.6.135.43 > MYIP.20252: P 1:198(197) ack 11 win 114 <nop,nop,timestamp 496755466 4214646201> (DF)
10:13:39.714271 193.0.6.135.43 > MYIP.20252: . 198:1646(1448) ack 11 win 114 <nop,nop,timestamp 496755469 4214646201> (DF)
10:13:39.714288 MYIP.20252 > 193.0.6.135.43: . ack 1646 win 14936 <nop,nop,timestamp 4214646201 496755466> (DF)
10:13:39.715107 193.0.6.135.43 > MYIP.20252: FP 1646:2209(563) ack 11 win 114 <nop,nop,timestamp 496755470 4214646201> (DF)
10:13:39.715123 MYIP.20252 > 193.0.6.135.43: . ack 2210 win 15821 <nop,nop,timestamp 4214646201 496755470> (DF)
10:13:39.715156 MYIP.20252 > 193.0.6.135.43: F 11:11(0) ack 2210 win 16384 <nop,nop,timestamp 4214646201 496755470> (DF)
10:13:39.720691 193.0.6.135.43 > MYIP.20252: . ack 12 win 114 <nop,nop,timestamp 496755476 4214646201> (DF)
If it doesn't send out stuff, I'd check the php.ini you're using if there's nothing limiting you there.