All in all, I don't like the URL based server location.  It makes for quite a confusing input mechanism, especially since you want to use a much more liberal parser that doesn't require anything resembling a URL.  (If you insisted that the full, valid URL be present, that wouldn't be so bad.)

You should replace this with a `server` argument, taking `example.com`, or `example.com:1234` as input, or as two distinct arguments, `server` and `port`.  The rest of it should be unnecessary.  That also allows eliminating all the complex regexp based URL hacking, etc, etc.