Add exception handling to socket operations.