When connecting as a client, don't create one socket, using the address
family of the first entry in the address list, and use that for all
entries; on most if not all platforms, an AF_INET socket can't be used
to connect to an IPv6 address and an AF_INET6 socket can't be used to
connect to an IPv4 address. Instead, construct a table of the entries
in the address list, sort it by address family, and cycle through the
entries. If there is no socket yet, create it based on the current
entry's address family; if there is a socket, but it's for a different
address family than the current entry's address family, close it and
open a new one.
If connecting fails for all addresses, don't just construct a long
barely-readable error message consisting of the full errors for each
failure. Instead, construct one that, for each error code, has a list
of the addresses that got that error code; if all the failures had the
same error code, just show the host name, not the complete list of
addresses.
Clean up some error handling routines - fix names, allow some to take a
printf-style argument list, etc..