private bool ModuleHasRegisteredPorts(VModule module)
 {
     lock (this)
     {
         foreach (VModule moduleWithActivePorts in registeredPorts.Values)
         {
             if (module.Equals(moduleWithActivePorts))
             {
                 return true;
             }
         }
     }
     return false;
 }
        /// <summary>
        /// Deregister a port to declare it unavailable for use by other modules
        /// </summary>
        /// <param name="port">The port to deregisted</param>
        /// <param name="owner">The module to which the port belongs</param>
        /// <returns></returns>
        public int DeregisterPort(VPort port, VModule module)
        {
            ResultCode result;

            lock (this)
            {
                if (registeredPorts.ContainsKey(port))
                {
                    if (module.Equals(registeredPorts[port]))
                    {
                        logger.Log("deregistering port: {0}", port.ToString());
                        registeredPorts.Remove(port);

                        result = ResultCode.Success;
                    }
                    else
                    {
                        logger.Log("got port deregisteration for {0} from a non-owner {1}", port.ToString(), module.ToString());

                        result = ResultCode.Failure;
                    }
                }
                else
                {
                    logger.Log("got deregisteration for unregistered port: {0}", port.ToString());

                    result = ResultCode.PortNotFound;
                }
            }

            if (result == ResultCode.Success)
            {
                
                BroadcastPortDeregistration(port, module); 
                // broadcast needs to be asyncronous because the module's addin (and appdomain, ports, etc) will be wiped
                /*
                System.Threading.Thread newThread = new System.Threading.Thread(delegate() { 
                 //   System.Threading.Thread.Sleep(Settings.portRegisterDelay); 
                    BroadcastPortDeregistration(port, module); });
                newThread.Name = "Deregister Port " + port;
                newThread.Start(); */
            }

            return (int)result;
        }