BytesRoad.Net.Sockets.Socket_Socks4.PrepareCmd C# (CSharp) Method

PrepareCmd() private method

private PrepareCmd ( EndPoint remoteEP, byte cmdVal ) : byte[]
remoteEP System.Net.EndPoint
cmdVal byte
return byte[]
        byte[] PrepareCmd(EndPoint remoteEP, byte cmdVal)
        {
            int userLength = 0;
            if(_proxyUser != null)
                userLength = _proxyUser.Length;

            IPEndPoint ip = (IPEndPoint)remoteEP;
            byte[] cmd = new byte[8 + userLength + 1];
            cmd[0] = 4;
            cmd[1] = cmdVal;
            cmd[2] = (byte)((ip.Port & 0xFF00) >> 8);
            cmd[3] = (byte)(ip.Port & 0xFF);

            long ipAddr = ip.Address.Address;
            cmd[7] = (byte)((ipAddr & 0xFF000000) >> 24);
            cmd[6] = (byte)((ipAddr & 0x00FF0000) >> 16);
            cmd[5] = (byte)((ipAddr & 0x0000FF00) >> 8);
            cmd[4] = (byte)((ipAddr & 0x000000FF));

            if(userLength > 0)
                Array.Copy(_proxyUser, 0, cmd, 8, userLength);
            cmd[8 + userLength] = 0;
            return cmd;
        }