Tcp link unit test

Started out writing a unit test for TCPLink and fixed a bunch if other broken stuff I found along the way. Bunch of commits, but most are small. Just trying to separate the changes for easier code review. Built and tested on OSX, Linux, Windows.