If i'm reading this correctly, you're setting the currentSequenceNumber again after this sleep. So you'll have the same problem. You should get the currentSequenceNumber once, and if it's > lastKnown, sleep for 1, then get lastKnown again and do the compare. If it's still bad then do the replay