Refactor basic TCP handling a bit