Always close a socket regardless of exceptions

This patch makes sure the socket is always closed. There are times `self.sock.shutdown()` raises `error: [Errno 107] Transport endpoint is not connected` which means the following line where the socket is closed, is skipped.