Improve error handling on TCP