Shutting down sockets and waiting for threads to end