Moved select into a single function and reduced branching in InThreadSendRecv

Includes the changes introduced in the master branch