But if it "hangs" does it hang in the loop? You've code showing it exits the loop, but do you know it entered the loop? What I would do is first determine where the program "hangs": print a message before fetching a page; print a message after the page was retrieved; print a message before attempting the pattern; print a message when entering the body of the while; print a message just before exiting the body; print a message when exiting the while construct.
And to avoid buffer problems, print those messages to STDERR.