function alloc($socket) {
		if (is_bool($buf=$this->read($socket)))
			return;
		// Get WebSocket headers
		$hdrs=[];
		$EOL="\r\n";
		$verb=NULL;
		$uri=NULL;
		foreach (explode($EOL,trim($buf)) as $line)
			if (preg_match('/^(\w+)\s(.+)\sHTTP\/1\.\d$/',
				trim($line),$match)) {
				$verb=$match[1];
				$uri=$match[2];
			}
			else
			if (preg_match('/^(.+): (.+)/',trim($line),$match))
				// Standardize header
				$hdrs[
					strtr(
						ucwords(
							strtolower(
								strtr($match[1],'-',' ')
							)
						),' ','-'
					)
				]=$match[2];
			else {
				$this->close($socket);
				return;
			}
		if (empty($hdrs['Upgrade']) &&
			empty($hdrs['Sec-Websocket-Key'])) {
			// Not a WebSocket request
			if ($verb && $uri)
				$this->write(
					$socket,
					'HTTP/1.1 400 Bad Request'.$EOL.
					'Connection: close'.$EOL.$EOL
				);
			$this->close($socket);
			return;
		}
		// Handshake
		$buf='HTTP/1.1 101 Switching Protocols'.$EOL.
			'Upgrade: websocket'.$EOL.
			'Connection: Upgrade'.$EOL;
		if (isset($hdrs['Sec-Websocket-Protocol']))
			$buf.='Sec-WebSocket-Protocol: '.
				$hdrs['Sec-Websocket-Protocol'].$EOL;
		$buf.='Sec-WebSocket-Accept: '.
			base64_encode(
				sha1($hdrs['Sec-Websocket-Key'].WS::Magic,TRUE)
			).$EOL.$EOL;
		if ($this->write($socket,$buf)) {
			// Connect agent to server
			$this->sockets[(int)$socket]=$socket;
			$this->agents[(int)$socket]=
				new Agent($this,$socket,$verb,$uri,$hdrs);
		}
	}