in reply to print() on closed filehandle STDOUT after printing to socket

$socket.close();

You probably meant $socket->close() — the dot operator is string concatenation in Perl.   close() without any argument will close the currently selected filehandle — presumably STDOUT in this case...