Handle lost connections to the base station without crashing

Also, cleanup some of the socket code, now that we know it works