memif: complete refactor of socket handling code