Fix infinite loop issue

This addresses GH #481 

I don't know how well this will work on Windows, if at all.  Something like http://msdn.microsoft.com/en-us/library/aa364953.aspx might provide useful for platform compatability.