No offense intended, but that code of yours is pretty ugly. And you're un-necessarily trying to re-invent a wheel that already exists and works perfectly.
Personally, I would be using Net:SCP.
Or alternatively, you might want to use Net:FTP
--Darren
Comment on Re: How to upload file without using FTP?