I may be wrong here but I think this patch is not correct. If the timeout value is != 0 and there is no data, we continue to the code below:
if (timeout && !t)
{
t = addtimeout (p, (long)timeout, unselectme);
timeout = 0;
}
and in the next iteration (descriptor) you'll terminate even though you should wait & retry. Whole function / loop is a dirty mess, several examples of terrible programming practices.