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.
My understanding is that we might want to actually prefer 'localhost' on top. At least, machines that host the API should connect to their on Mongo instance preferentially (thus localhost).
But perhaps this sorting isn't relevant at this level.