Don't do it by hand. That's your best bet. Try something like POE or Net::Server. That's not directly answering your question, but it probably addresses your application as a whole. There are actually a lot of really good choices for handling sockets for you and writing it yourself usually causes more headaches than it's worth.