private void SendCommand(NetworkStream nStream, byte command, string destinationHost, int destinationPort) { byte aTyp = GetAddressType(destinationHost); byte[] dstAddr = HostHelper.GetHostAddressBytes(aTyp, destinationHost); byte[] dstPort = HostHelper.GetPortBytes(destinationPort); // +----+-----+-------+------+----------+----------+ // |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT | // +----+-----+-------+------+----------+----------+ // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ byte[] request = new byte[4 + dstAddr.Length + 2]; request[0] = VersionNumber; request[1] = command; request[2] = Reserved; request[3] = aTyp; dstAddr.CopyTo(request, 4); dstPort.CopyTo(request, 4 + dstAddr.Length); nStream.Write(request, 0, request.Length); // +----+-----+-------+------+----------+----------+ // |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | // +----+-----+-------+------+----------+----------+ // | 1 | 1 | X'00' | 1 | Variable | 2 | // +----+-----+-------+------+----------+----------+ byte[] response = new byte[255]; nStream.Read(response, 0, response.Length); byte reply = response[1]; if (reply != CommandReplySucceeded) { HandleCommandError(reply); } }