Don't assume recv() returns well-framed packets from X server. The code is still horribly broken in its buffer handling, but this fixes the worst immediate problem.