     } else {
       rawSocket = new Socket();
       if (scheme == "wss") {
-        tlsConfig= new TLSConfig(TLSEngine.CLIENT,
-            null, null, null, null, null,
-            TLSSecurityParameters.PROTOCOL_VERSION);
-        tlsConfig.trustAllCertificates = true;
-        tlsConfig.ignoreCommonNameMismatch = true;
-        tlsSocket = new TLSSocket();
-        tlsSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
-        socket = tlsSocket;
+          if (this.useFlashSecureSocket) {
+              logger.log('using internal SecureSocket');
+              secureSocket = new SecureSocket();
+              secureSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
+              rawSocket = socket = secureSocket;
+          }
+          else {
+              tlsConfig= new TLSConfig(TLSEngine.CLIENT,
+                      null, null, null, null, null,
+                      TLSSecurityParameters.PROTOCOL_VERSION);
+              tlsConfig.trustAllCertificates = true;
+              tlsConfig.ignoreCommonNameMismatch = true;
+              tlsSocket = new TLSSocket();
+              tlsSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
+              socket = tlsSocket;
+          }
       } else {
         rawSocket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
         socket = rawSocket;

    logger.log("connected");

    if (scheme == "wss") {
      logger.log("starting SSL/TLS");
      tlsSocket.startTLS(rawSocket, host, tlsConfig);
    }
    
    var defaultPort:int = scheme == "wss" ? 443 : 80;

  
  private var logger:IWebSocketLogger;
  private var base64Encoder:Base64Encoder = new Base64Encoder();

  private var useFlashSecureSocket:Boolean = false;
  
  public function WebSocket(
      id:int, url:String, protocols:Array, origin:String,
      proxyHost:String, proxyPort:int,
      cookie:String, headers:String,
      logger:IWebSocketLogger,
      useFlashSecureSocket:Boolean = false) {
    this.logger = logger;
    this.id = id;
    this.url = url;
