Fix race condition in connectToParentWindow.

Previously there was time for a snapshot to come in after the "hello"
was sent but before the onmessage listener was attached. By moving the
channel create into the setImmediate callback we can avoid this, and
also unify code between the cases.