I don't know exactly which part causes the crash, but I assume you'd be familiar with it. You should use a try/catch blocks. I assume some kind of exception is thrown when it fails to connect. Put the code you think presents the issue in the "try" block. Then put what you want to do when the issue comes up in the "catch" block. This way, when it throws an exception, you can handle it rather than crash.