Use URI to build URIs and hostname= to wrap ipv6 addresses in brackets.

See: https://bugs.ruby-lang.org/issues/3788
Related: https://www.ietf.org/rfc/rfc2732.txt