refactor socket helper functions