Fixed a whole heap of bugs in the tcp protocol