socket: Refactor socket classes to minimize repeated code.