Added tested and working error handling for sockets.