in reply to POE: How can I detect server closed my connection?

From your code, you seem to have a $socket variable. Try the connected method of IO::Socket.
if ($socket->connected) { } #or if ! ($socket->connected) { }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh