use strict; use warnings; use IO::Socket::SSL qw(debug4); my $_pid; my $connection = IO::Socket::SSL->new( PeerHost => 'local.btc-node', PeerPort => 7566, Proto=> 'tcp', Timeout => 8, SSL_verify_mode => 0x00 ) or die($!); while(<$connection>) { if(/action1/) { # do something, no problem } elsif(/tick\s+([^\d\s\t]+)/) { my $currency = $1; tick ($connection, $currency); } } sub tick { my $socket = shift; my $currency = shift; if ($_pid = fork) { waitpid($_pid, 0); } else { if (fork) { exit; } else { my $response =`tick $currency 2>&1 3>&1`; $socket->print($response . "\n"); # without this line $connection doesn't break on the second call exit; } } }