Fixed bug.

Now can retry connection after fail.
Added flags to indicate if popup died.
Completed code to complete refactor and make popups work again.
Moved input steam to while loop. Fixed problem when input stream is reused.
Fixed possible concurrency problem.