Generalized the waiting for message function