I don't know AppleScript but it seems this should be moved inside the loop. What happens if it's already connected?