This function will handle the creation of remote debugging port (and probably others that will end up in this function) that seems to me shouldn't be applied multiple times. I'm starting to think you need to make the distiction clearer here. What are things you handle multiple times, and what  things you handle only in the first run. Having separated functions might help.