for(;;) { if ($array[$incr] eq "[SEND]") { if( .. whatever .. ) { $incr++; outer(); next; } } elsif ($array[$incr] eq "[RECV]") { ... $incr++ inner(); next; } } sub outer { my $msg=$method; print "\nSending message:$msg"; $MySocket->send($msg); } sub inner { print "Waiting for Response:\n"; $MySocket->recv ($text,1000);#///Recv print "\nReceived message From:$text"; }