Instead of sleeping wait for ports to be open

After starting selenium and phantomjs run a loop and wait for the ports to be active for those services.