title: SignalR change port - SelfHost
body: <p>I am trying to change the port of my server using a remote request.
  When I do that it seems that everything is ok but when I try to connect I get an exception.</p>
  
  <p>If I close(terminate process) and then open(start process) the server with the new port everything works fine(the clients manages to connect).</p>
  
  <p><strong>Server:</strong></p>
  
  <pre><code>internal class Program
  {
      private static IDisposable _webApp = null;
      static void Main(string[] args)
      {
          var uri = Start(8089);
          Stop();
          var uri = Start(8089);
          Console.WriteLine("Press enter to exit");
          Console.ReadLine();
      }
  
      private static string Start(int port)
      {
          var options = new StartOptions();
          var uri = string.Format("http://*:{0}/", port);
          options.Urls.Add(uri);
          options.Settings.Add(typeof(ITraceOutputFactory).FullName, typeof(NullTraceOutputFactory).AssemblyQualifiedName);// disable built-in owin tracing by using a null traceoutput
          var startup = new Startup
              {
                  Restart = Restart
              };
          _webApp = WebApp.Start(options, startup.Configuration);
          Trace.Listeners.Remove("HostingTraceListener"); // remove the built-in owin listener
          Console.WriteLine("Server running at " + uri);
          return uri;
      }
  
      private static void Stop()
      {
          _webApp.Dispose();
      }
  
      private static string Restart(int port)
      {
          Stop();
          var uri = Start(port);
          return uri;
      }
  
      internal class NullTraceOutputFactory : ITraceOutputFactory
      {
          public TextWriter Create(string outputFile)
          {
              return StreamWriter.Null;
          }
      }
  }
  
  [HubName("Configuration")]
  public class ConfigurationHub : Hub
  {
      private Func&lt;int, string&gt; _restart;
  
      public ConfigurationHub(Func&lt;int, string&gt; restart)
      {
          _restart = restart;
      }
  
      public void ChangePort(int port)
      {
          Console.WriteLine("Port: " + port);
          _restart(port);
      }
  }
  
  internal class Startup
  {
      public Func&lt;int, string&gt; Restart;
  
      public void Configuration(IAppBuilder app)
      {
          GlobalHost.DependencyResolver.Register(typeof(ConfigurationHub),
                  () =&gt; new ConfigurationHub(Restart));
          app.UseCors(CorsOptions.AllowAll);
          //app.UseWelcomePage();
          var hubConfiguration = new HubConfiguration
          {
              EnableJSONP = true,
  #if DEBUG
              EnableDetailedErrors = true
  #endif
          };
  
          app.MapSignalR(hubConfiguration);
      }
  }
  </code></pre>
  
  <p><strong>Client:</strong></p>
  
  <pre><code>public partial class Form1 : Form
  {
      private IHubProxy _configurationProxy = null;
      private HubConnection _hubConnection;
      private const string CATEGORY = "ABCD";
  
      public Form1()
      {
          InitializeComponent();
          CreateProxy(8089);
          _hubConnection.Start().Wait();
      }
  
      private void btnChangePort_Click(object sender, EventArgs e)
      {
          var newPort = Int32.Parse(btnChangePort.Tag as string);
          _configurationProxy.Invoke("ChangePort", newPort);
          _hubConnection.Stop();
          _hubConnection.Dispose();
          _hubConnection = null;
          CreateProxy(newPort);
          _hubConnection.Start().Wait(); // &lt;= Exception when clicking the button
      }
  
      private void CreateProxy(int newPort)
      {
          //var address = "127.0.0.1";
          var address = "192.168.0.100";
          var port = newPort;
          var config =
              new
                  {
                      Name = "Configuration",
                      Pattern = "http://{0}:{1}",
                      Address = address,
                      Port = newPort,
                      QueryString = new Dictionary&lt;string, string&gt; { { "version", "1.0" } },
                      Methods = new[]
                          {
                              "notifyWrongVersion",
  #if ERROR
                                  "notifyConnected",
  #endif
                              //"status",
                              "notifyByeBye"
                          },
                      MethodsCallbacks = new Action[]
                          {
                              () =&gt; Console.WriteLine("{0}: You are using the wrong version!", CATEGORY),
  #if ERROR
                                      () =&gt; Trace.WriteLine("Connected to server! HOORAY!", Categories.CATEGORY_THIS),
  #endif
                              () =&gt;
                              Console.WriteLine("{0}: Bye bye from the server. Thank you for discnnecting.", CATEGORY)
                          },
                  };
  
          var url = String.Format(config.Pattern, config.Address, config.Port);
  
          _hubConnection = new HubConnection(url);
          _hubConnection.TraceLevel = TraceLevels.All;
          _hubConnection.TraceWriter = Console.Out;
          _configurationProxy = _hubConnection.CreateHubProxy(config.Name);
      }
  }
  </code></pre>
  
  <p><strong>Exception:</strong>
  <img src="https://i.stack.imgur.com/23dvJ.jpg" alt="Exception"></p>
  
  <p><strong>Scenario:</strong><br>
  Started with port 8089 on client and server<br>
  Pressed the button on client and server changed to 8088<br>
  Closed the client<br>
  Opened the client with port 8088.  </p>
  
  <p><strong>Client Logging:</strong></p>
  
  <blockquote>
    <p>14:04:18.2450308 - null - ChangeState(Disconnected, Connecting)
    14:04:18.7762877 - 708038b9-66b0-4cc2-95ac-de45411e453c - WS
    Connecting to:
    ws://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&amp;transport=webSockets&amp;connectionData=[{"Name":"Configuration"}]&amp;connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2By%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D
    14:04:23.8220044 - 708038b9-66b0-4cc2-95ac-de45411e453c - Auto: Failed
    to connect to using transport webSockets. System.TimeoutException:
    Transport timed out trying to connect 14:04:23.8220044 -
    708038b9-66b0-4cc2-95ac-de45411e453c - SSE: GET
    <a href="http://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&amp;transport=serverSentEvents&amp;connectionData=[" rel="nofollow noreferrer">http://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&amp;transport=serverSentEvents&amp;connectionData=[</a>{"Name":"Configuration"}]&amp;connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2By%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D
    14:04:23.9157549 - 708038b9-66b0-4cc2-95ac-de45411e453c - WS:
    OnClose() 14:04:23.9157549 - 708038b9-66b0-4cc2-95ac-de45411e453c -
    SSE: OnMessage(Data: initialized) 14:04:28.8486354 -
    708038b9-66b0-4cc2-95ac-de45411e453c - Auto: Failed to connect to
    using transport serverSentEvents. System.TimeoutException: Transport
    timed out trying to connect 14:04:28.8642594 -
    708038b9-66b0-4cc2-95ac-de45411e453c - LP Connect:
    <a href="http://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&amp;transport=longPolling&amp;connectionData=[" rel="nofollow noreferrer">http://192.168.250.9:8088/signalr/connect?clientProtocol=1.4&amp;transport=longPolling&amp;connectionData=[</a>{"Name":"Configuration"}]&amp;connectionToken=AQAAANCMnd8BFdERjHoAwE%2FCl%2BsBAAAAVaJK6bsSpkyFVlk1Ej463gAAAAACAAAAAAADZgAAwAAAABAAAAAcSxXM3loQOfD31c8mYLdqAAAAAASAAACgAAAAEAAAAISDmfaiIg%2By%2FVt3bBI%2BZkAoAAAAd%2FngqVkiFGH55BN9NOW4nljYZzHBBwoTLxNwrj%2B31AiShmmgS6euMBQAAADjFYeiwsjjJ%2BRVX92GYdozAkKthw%3D%3D
    14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - Auto: Failed
    to connect to using transport longPolling. System.TimeoutException:
    Transport timed out trying to connect 14:04:33.8799023 -
    708038b9-66b0-4cc2-95ac-de45411e453c - Disconnected 14:04:33.8799023 -
    708038b9-66b0-4cc2-95ac-de45411e453c -
    Transport.Dispose(708038b9-66b0-4cc2-95ac-de45411e453c)
    14:04:33.8799023 - 708038b9-66b0-4cc2-95ac-de45411e453c - Closed</p>
  </blockquote>
  
  <p>after 3 times getting this error I get also this:</p>
  
  <blockquote>
    <p>14:14:24.0350106 - null - OnError(System.TimeoutException: The client
    has been inactive since 31/05/2015 14:13:27 and it has exceeded the
    inactivity timeout of 00:00:50. Stopping the connection.)</p>
  </blockquote>