in reply to When do I stop reading?

I have done a few socket programs using perl as a client. I have never had a problem, in that the server has sent me some sort of eof so that I know when to stop reading. If that doesn't work, check your output -- does the server send you something like "done" that you can key off of?

Another thought is to use alarm() to limit the amount of time that you're connected.

Hope that helps.