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.
Is it possible for you to have your own public address in the list (10.0.3.1, etc)
I guess if we know that we only connect to "localhost" on the first connection to the database after bootstrap then we should be ok. I'm slightly concerned about upgrades that may not have been using only 127.0.0.1 in the past.

Given all the values reused here, we probably want to just create them as "constants". So pull these out to:

portStr := strconv.Itoa(p.Port)
localhostIPv4 := net.JoinHostPort("127.0.0.1", portStr)
localhostIPv6 := net.JoinHostPort("::1", portStr)

and then we can do:

switch addr {
  case localhostIPv4:
  case localhostIPv6:
    continue
}
(or addr == localhostIPv4 || addr == localhostIPv6 as you prefer)
And then you can use those strings again later.