restructure code to retry connection asynchronously