title: AS3: negotiating FTP-ES connection
body: <p>I'm trying to modify an ActionScript 3 FTP library to support explicit secure (FTP-ES) connections.</p>
  
  <p>With FTP-ES, the client initially makes an insecure FTP connection, then explicitly asks the server to switch to a secure SSL/TLS connection.</p>
  
  <p>Right now, I initially connect to the FTP server with a regular Socket, ask the server to switch to TLS, and then try connect with a SecureSocket after the server returns FTP code 234:</p>
  
  <pre><code>if (code.indexOf('220 ') == 0 || code.indexOf('220-') == 0) { //Send user name
      if (_useTLS) {
          trace("AUTH TLS");
          cmdSocket.writeUTFBytes('AUTH TLS\r\n');
      } else {
          cmdSocket.writeUTFBytes('USER ' + _username + '\r\n');
      }
      cmdSocket.flush();
  }
  
  if (responseCode.indexOf('234 ') == 0) { //Auth OK, expecting SSL/TLS negotatiation
      if (SecureSocket.isSupported) {
          secureCommandSocket = new SecureSocket();
          secureCommandSocket.addEventListener(ProgressEvent.SOCKET_DATA, onReceivedSCmd);
          secureCommandSocket.addEventListener(Event.CONNECT, onConnectSCmd);
          secureCommandSocket.addEventListener(IOErrorEvent.IO_ERROR, onSocketError);
          secureCommandSocket.addEventListener(Event.CLOSE, onCloseSCmd);
          secureCommandSocket.connect(_host, _port);
      } else {
          throw new Error("Secure socket is not supported on this platform");
      }
  }
  </code></pre>
  
  <p>However, the socket returns an IOError 2031 (socket error). Here's the output:</p>
  
  <pre><code>MESSAGE: CONNECTED TO FTP
  MESSAGE: Received command: 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
  220-You are user number 1 of 50 allowed.
  
  AUTH TLS
  MESSAGE: Received command: 234 AUTH TLS OK.
  
  MESSAGE: 2031:Error #2031: Socket Error. URL: xx.xx.xxx.xx
  ERROR: Connection failed
  </code></pre>
  
  <p>With no more information about what's going wrong, I have no idea why the secure socket connection is failing. Is it because I'm trying to switch from a regular Socket to a SecureSocket mid-connection? If so, is there any other way to handle this in AS3?</p>