You pass &tmp_input&tmp_input to select but then pass &input&input to the FD_ISSETsFD_ISSETs. These two fd_setfd_set objects are different. The input fd_setfd_set is set once, so FD_ISSETFD_ISSET may well lie to you and your reads will fail. I am surprised no one saw this.
stuart