Network code cleanup

This includes a change to ensure that the listening socket is closed
immediately after accepting a connection.