Improve socket error handling