Merge improved socket reading code by Tobias Limmer & Thomas Schreck
Remove dead code