Moved opening of the stream after ProcessMessages