restructuring server side tcp connection code