in reply to Problems reading back from socket

Why are you making your TCP socket nonblocking (with the Blocking => 0 parameter)? A nonblocking socket will return if there's nothing to read; I suspect that when you try to read it with my $data=<$sock>, there's no data to read, so it returns false and the whole program exits. I suspect removing this parameter would solve your problem, but I haven't tested it.

You could confirm this by adding debugging output to your program, or running it in the Perl debugger.

I'd also recommend adding more error checking; right now you wouldn't notice if the socket creation failed, or the prints to the socket failed, since you're not checking their return values. Checking the return value from close also sometimes shows errors that were delayed, or caused the socket to close.