Refactor socket helper functions.