Refactor socket creation