Further improve and simplify socket error handling