Handle sequence numbers better. In particular, track full 32-bit sequence numbers in both directions, and generate an internal GetInputFocus request if 0x2000 sequence numbers pass without the server sending us an event or reply (so we don't lose track of the high 16 bits by having the low 16 bits wrap completely around undetected). 0x2000 is admittedly paranoid; 0x8000 should be enough. But I think even 0x2000 is extremely unlikely to happen in practice.