clean up socket handling

- moved socket code to its own file to contain the
  logic behind a set of APIs