private static int GetFreePort(Type typeClass)
        {
            currentPort++;

            if (currentPort >= endPort)
            {
                currentPort = startPort;
            }

            var port = currentPort;

            while (FileExistsAndCantBeRemoved(RemoteInstanceRunner.GetStatusFilePathPrefix(typeClass.Name, port)))
            {
                log.DebugFormat("Status file for port {0} exists, using next port", port);
                port++;
            }

            return(port);
        }
        /// <summary>
        /// Connects to existing remote instance of starts server and creates a new one.
        /// </summary>
        /// <typeparam name="TServiceInterface"></typeparam>
        /// <typeparam name="TServiceClass"></typeparam>
        /// <returns></returns>
        public static TServiceInterface CreateInstance <TServiceInterface, TServiceClass>() where TServiceInterface : class
        {
            // start server and create a new instance
            var port = GetFreePort(typeof(TServiceClass));

            var interfaceType = typeof(TServiceInterface);

            log.DebugFormat("Starting server at port {0}, type {1}", port, interfaceType.Name);

            var runner = new RemoteInstanceRunner(interfaceType, typeof(TServiceClass), port);

            runner.Exited += runner_Exited;
            runner.Run();

            var remoteInstance = runner.GetRemoteInstance <TServiceInterface>();

            remoteInstances.Add(remoteInstance);

            remoteInstanceServerRunners.Add(runner);

            return(remoteInstance);
        }