Try to fix exception handling when closing sockets.