Move some of the socketpair reading to its own function