move socket code out