If you were using
IO::Socket then this might be a bit easier since that class provides built-in read/write routines (part of
IO::Handle, of course).
I'm not sure about these disconnects, but you have to be careful with your buffer. If you cram too much output at the client at once, you can flood it, and this will close the connection. Make sure you send output
only when the client is ready to receive. They call this flow-control. You can check if a socket is ready to send, or wait until it is.