Network code cleanup

This includes a change to ensure that the listening socket is closed
immediately after accepting a connection as well as making full use of the getaddrinfo api.