title: ActionScript 3.0 sockets can't reconnect
body: <p>I'm having trouble connecting (via the Socket and XMLSocket classes) to a TCP listener after a connect timeout.</p>
  
  <p>I have a flash applet that needs to maintain a TCP connection to a server. I have implemented timeouts using Timers, a ping-pong system, etc. However, there's a problem. Upon calling Socket.connect(), the flash player (9.0.115 and many other stables before that) sends 3 connection requests by way of SYN packets, with some time in between. If none of those are replied to (e.g. because the server is down), I cannot get the applet to [attempt to] connect to the server. Ever. That is, within the lifetime of the applet.</p>
  
  <p>To clarify and/or summarize:</p>
  
  <p>For any host/port pair given to Socket.connect() or XMLSocket.connect(), if the call fails, any subsequent connect() calls to any other Socket (or XMLSocket) instances <strong>within the lifetime of the Flash applet</strong> to the same host/port pair get ignored. (At least as far as I can tell using a packet sniffer.)</p>
  
  <p>I have tried calling numerous Socket methods, destroying¹ and recreating the objects, using a pool of Sockets, and various other methods I can't remember right now; all to no avail.</p>
  
  <p>My current solution is to notify the parent webpage through a JavaScript call and let it reload my applet. It's not a pretty solution, and I'm not about to implement workarounds for the problems <em>it</em> causes, just because Flash can't handle socket connections properly.</p>
  
  <p>I must be missing something very simple.</p>
  
  <p>Any ideas?</p>
  
  <p>1: I know you can't really destroy objects; I just remove all references to them and hope for the best. I haven't tried to explicitly invoke the GC in this case. (Though I think I did try putting the Socket inside an Array and using <em>delete</em>.)</p>
  
  <hr>
  
  <p>Yes, it works as expected if the connection is successful (even if the connection drops later on.)</p>
  
  <p>The only event to trigger this is the case when the server doesn't respond at all; it's as if Flash marks the host/port combination as "offline" and doesn't bother sending any more packets to it for the lifetime of the applet.</p>
  
  <p>I suspect an active refusal of the connection (e.g. host is online but not listening to the port) doesn't cause this.</p>
  
  <p>I get no error message or feedback of any other kind from the Socket.</p>
  
  <p>Have you ever called connect() more than once to the same host/port pair, when the first one failed?
  How did you know the first connect() failed? And before subsequent connect() calls, did you do anything to reset the socket?</p>