Make exception handling on sockets more robust.