I think what you really want to do is connect, grab all of the messages, determine if the message you're looking for is there and do something based on that. Is this correct? Do you want the script to continually reconnect and retry if it doesn't find what it's looking for? If so, do you want to to keep trying after it's foud it's first match?