I know that deep down a little voice is screaming at you that closing a socket is not the proper way to inform your script that there is no more data to be received...
Use fgets instead, or alter your server so that it first sends a string containing the length of the rest of the data.
Then you can receive that string using fgets, and then you can use fread() to read exactly as many bytes as the server is going to send. Then fread will stop when it received enough bytes, and the sender will stop at the same time because it sent all it had to send.
A forum, a FAQ, what else do you need?