I would look at use threads; (perlthrtut) and study how the semaphores work, with the basic idea that the connection is run in a separate thread that is managed by your program. I have not had to manage semaphores before, so I have no experience to help guide you on this.
Comment on Re: Net::SSH::Perl - Breaking out of a running command?