move tcp code to generic socket code