unset($arr[$key]);
    }
}
list($host, $port) = explode(':', $params['Host']);
if (empty($params['Origin']) && !empty($params['Sec-WebSocket-Origin'])) {
    $params['Origin'] = $params['Sec-WebSocket-Origin'];
}
if (preg_match("/GET (.*) HTTP/", $str, $match)) {
    $r = $match[1];
}
$answer = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n";
$answer .= "Upgrade: WebSocket\r\n";
$answer .= "Connection: Upgrade\r\n";
$answer .= "Sec-WebSocket-Origin: {$params['Origin']}\r\n";
$answer .= "Sec-WebSocket-Location: ws://{$params['Host']}{$r}\r\n";
if (isset($params['Sec-WebSocket-Key1']) && isset($params['Sec-WebSocket-Key2']) && !empty($params['Sec-WebSocket-Key1']) && !empty($params['Sec-WebSocket-Key2'])) {
    $SESS[$keyINsock]['websock_encode'] = false;
    $answer .= "\r\n" . websock_calcKey($params['Sec-WebSocket-Key1'], $params['Sec-WebSocket-Key2'], $l8);
} else {
    $SESS[$keyINsock]['websock_encode'] = true;
    $params['Sec-WebSocket-Accept'] = base64_encode(sha1($params['Sec-WebSocket-Key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
    $answer .= "Sec-WebSocket-Accept: {$params['Sec-WebSocket-Accept']}\r\n";
    $answer .= "Access-Control-Allow-Origin: {$params['Origin']}\r\n";
    $answer .= "Access-Control-Allow-Credentials: true\r\n";
    $answer .= "Access-Control-Allow-Headers: content-type\r\n";
    $answer .= "\r\n";
}
sockSend($keyINsock, $answer, false);
//dissconect($keyINsock);
$SESS[$keyINsock]['websock'] = true;
unset($params);
 if (count($sock) > 0) {
     //Обработка массива сессий
     //require (dirname(__FILE__).'/include/sess.php');
     $read = $sock;
     $w = $e = null;
     if (false === socket_select($read, $w, $e, 0)) {
         echo "socket_select() failed, reason: " . socket_strerror(socket_last_error()) . "\n";
     } else {
         foreach ($read as $key => $nowread) {
             //Читает сокет. Находит ключ $keyINsock.
             require dirname(__FILE__) . '/include/socket_read.php';
             // NOTICE:Клиент отключился
             if ($strlen == 0) {
                 require dirname(__FILE__) . '/include/disconnect.php';
             } elseif ($str == '<policy-file-request/>') {
                 sockSend($keyINsock, '<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>');
                 disconnect($keyINsock);
             } elseif (!isset($SESS[$keyINsock]['websock']) && substr($str, 0, 3) == 'GET') {
                 require dirname(__FILE__) . '/include/websock_hellow.php';
             } elseif (isset($SESS[$keyINsock]['websock'])) {
                 require dirname(__FILE__) . '/include/websock_read.php';
             } elseif ($str == "stp" || $str == 'shutdown') {
                 shutdownSev($mainSocket, $sock);
                 break;
             } else {
                 echo "Not command\n";
             }
         }
     }
 }
 //для разгрузки процессора