Maybe it would be better to just do the loop/waiting for the test below instead of counting messages?  It seems like that should be more robust than counting messages.