title: mono c# strange Address already in use error
body: <p>Its my first time using mono, I have compiled some ready code and tried to use on my linux host yet i get strange error. I am not really experienced with C# like experts but i can manage code.</p>
  
  <p><em>I forgot to mention, there isnt any single app using port 5031. I even changed port to random numbers yet it still gives same error.</em></p>
  
  <pre><code>Unhandled Exception:
  System.Net.Sockets.SocketException: Address already in use
    at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.Sockets.TcpListener.Start () [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.NetTcp.TcpChannelListener`1[System.ServiceModel.Channels.IDuplexSessionChannel].OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Dispatcher.ListenerLoopManager.Setup (TimeSpan openTimeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.ServiceHostBase.OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open () [0x00000] in &lt;filename unknown&gt;:0
    at H_Auth.AuthSvc.Main (System.String[] args) [0x00000] in &lt;filename unknown&gt;:0
  [ERROR] FATAL UNHANDLED EXCEPTION: System.Net.Sockets.SocketException: Address already in use
    at System.Net.Sockets.Socket.Bind (System.Net.EndPoint local_end) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.Sockets.TcpListener.Start (Int32 backlog) [0x00000] in &lt;filename unknown&gt;:0
    at System.Net.Sockets.TcpListener.Start () [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.NetTcp.TcpChannelListener`1[System.ServiceModel.Channels.IDuplexSessionChannel].OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Dispatcher.ListenerLoopManager.Setup (TimeSpan openTimeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.ServiceHostBase.OnOpen (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open (TimeSpan timeout) [0x00000] in &lt;filename unknown&gt;:0
    at System.ServiceModel.Channels.CommunicationObject.Open () [0x00000] in &lt;filename unknown&gt;:0
    at H_Auth.AuthSvc.Main (System.String[] args) [0x00000] in &lt;filename unknown&gt;:0
  </code></pre>
  
  <p>and this is my code</p>
  
  <pre><code>namespace H_Auth
  {
    internal class AuthSvc
    {
      private static void Main(string[] args)
      {
              var adrs = new Uri[1];
              adrs[0] = new Uri("net.tcp://localhost:5031/");
              using (ServiceHost serviceHost = new ServiceHost(typeof (HBChannel), adrs))
              {
        try
        {
          serviceHost.AddServiceEndpoint(typeof (IA), (System.ServiceModel.Channels.Binding) new NetTcpBinding(SecurityMode.None), "Auth.svc");
          ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
          serviceHost.Description.Behaviors.Add((IServiceBehavior) metadataBehavior);
          ((ServiceHostBase) serviceHost).AddServiceEndpoint("IMetadataExchange", MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
          serviceHost.Open();
          string str = Regex.Match(((AssemblyFileVersionAttribute) Assembly.GetEntryAssembly().GetCustomAttributes(typeof (AssemblyFileVersionAttribute), false)[0]).Version, "^\\d+\\.\\d+").Value;
          Console.ForegroundColor = ConsoleColor.Green;
          Console.WriteLine("Revision " + str + "\r\n");
          Console.ResetColor();
          Console.WriteLine("press \"S\" for stats print");
          Console.WriteLine();
  
          ConsoleKeyInfo consoleKeyInfo = new ConsoleKeyInfo();
          while (consoleKeyInfo.Key != ConsoleKey.Enter)
          {
                      consoleKeyInfo = Console.ReadKey(true);
                      if (consoleKeyInfo.Key == ConsoleKey.S)
                      {
                        AuthImpl.Instance.RemoveExpiredSessions();
                         AuthSvc.PrintStats(AuthImpl.Instance.GetSessions);
                       }
          }
          serviceHost.Close();
        }
        catch (CommunicationException ex)
        {
          Logging.Ex(ex.Message);
          serviceHost.Abort();
        }
              }
        Console.ReadLine();
  
      }
  
      private static void PrintStats(List&lt;SessInfo&gt; Sessions)
      {
        Console.WriteLine("Current active sessions:");
        Dictionary&lt;string, int&gt; dictionary1 = new Dictionary&lt;string, int&gt;();
        foreach (SessInfo sessInfo in Sessions)
        {
          if (!dictionary1.ContainsKey(sessInfo.BotSignature))
          {
            dictionary1.Add(sessInfo.BotSignature, 1);
          }
          else
          {
            Dictionary&lt;string, int&gt; dictionary2;
            string botSignature;
            (dictionary2 = dictionary1)[botSignature = sessInfo.BotSignature] = dictionary2[botSignature] + 1;
          }
        }
        if (dictionary1.Count &gt; 0)
        {
          foreach (KeyValuePair&lt;string, int&gt; keyValuePair in dictionary1)
            Console.WriteLine(string.Format("'{0}': {1} user {2}", (object) keyValuePair.Key, (object) keyValuePair.Value, keyValuePair.Value &gt; 1 ? (object) "S" : (object) ""));
        }
        else
          Console.WriteLine("There is no active sessions");
      }
    }
  }
  </code></pre>