Well, as I said It is your code. Modified.
And the modification is the while loop. Which I put in lua tags above the code.
This is your while loop, which I guess gets stuck.
while not $connected do
wait(90, 110)
end
This is the modified one:
while not $connected do
wait(90, 110)
keyevent(VK_ESCAPE)
reconnect()
end