You need to check the rset
returned (modified) by select to see if the sockets are ready to read -- the bits will ALWAYS be set in socketfds
since that is your master set of sockets to wait for. So change the
if(FD_ISSET(Xsocket, &socketfds))
lines to
if(FD_ISSET(Xsocket, &rset))