Improve error handling with sockets.