Update your code with this new line and see what it prints out
if ( ("80" eq $sPort) or ("8080" eq $sPort) )
{
$sProtocol = "http";
}
new line
print "iControl URL: $sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi"
print "Credentials: $sUID : $sPWD";
I'd then take the output of that and try to open that URL in a browser with the exact same credentials. Cannot connect is almost always an error with the protocol (http vs https), the IP, or the port.
Also, I'm assuming you are running this from the same machine as you are successfully connecting to with the iRule Editor. Is that correct?