prefer-ipv6 honored by state when setting addresses

Added network.SortAddresses() and network.SortHostPorts()
which use the prefer-ipv6 flag to determine which addresses
or HostPorts come on top.

Changed machine.SetAddresses and SetMachineAddresses to
sort the addresses according to whether prefer-ipv6 flag
is set in the environ config.

A few other places where changed as needed, including the
caching of API endpoints in the CLI client and mongo addresses
used to connect to state.

This is the final step of the prefer-ipv6 flag implementation.
Localhost is tricky, as it can be resolved as IPv4 or IPv6, but having 127.0.0.1 on top shouldn't be a problem when prefer-ipv6 is false.