Why the script work only 1 time

it would still break with your code, but ok

not to mention your code causes memory leaks due to how many connections it would make