As Haukex suggests ... it should always be a mantra: "am I now trying to do 'Actum Ne Agas: A Thing Already Done?'" In this case the answer is definitely "yes." There are plenty of both high-level and low-level frameworks on CPAN which have already thoroughly solved this problem. The code has been very thoroughly tested and it works.
Comment on Re: How do I make a tcp socket heartbeat?