use IO::Socket::INET; my $socket = IO::Socket::INET->new( PeerAddr => 'localhost', PeerPort => '9000', Proto => 'tcp', Type => SOCK_STREAM ) or die "Error: cant create listening socket: $!\n"; $socket->autoflush(1); # so output gets there right away die "can't fork: $!" unless defined( $kidpid = fork() ); #parent process if ($kidpid) { while ( defined( my $line = <$socket> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } #the child process else { my $line = ; while ( defined($line) ) { print $socket $line; } } close($socket);