'c# custom icmp echo reply

I need to send reply with my custom message to a client on response to the client's echo request message. Code of my server:

IPAddress ipAddr = IPAddress.Parse(args[0]);
IPEndPoint ipMyEndPoint = new IPEndPoint(ipAddr, 0);
EndPoint myEndPoint = (ipMyEndPoint);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
socket.Bind(myEndPoint);
socket.IOControl(IOControlCode.ReceiveAll, BitConverter.GetBytes(1), null);
while (true)
{
    Byte[] ReceiveBuffer = new Byte[socket.ReceiveBufferSize];
    var nBytes = socket.ReceiveFrom(ReceiveBuffer, ReceiveBuffer.Length, SocketFlags.None, ref myEndPoint);
    var icmpType = ReceiveBuffer[ICMP_TYPE_OFFSET];
    if (icmpType == ICMP_TYPE_ECHO_REQUEST)
    {
        Console.WriteLine("Echo Request received from " + myEndPoint);
        Console.WriteLine("Received: {0} bytes", nBytes);
        if (nBytes > PAYLOAD_OFFSET)
        {
            var payLoadSize = nBytes - PAYLOAD_OFFSET;
            byte[] payLoad = new byte[payLoadSize];
            Array.Copy(ReceiveBuffer, PAYLOAD_OFFSET, payLoad, 0, payLoadSize);
            string msg = Encoding.ASCII.GetString(payLoad);
            Console.WriteLine("Data hex: {0}", BitConverter.ToString(payLoad));
            Console.WriteLine("Data text: {0}", msg);
        }
        Console.WriteLine("---------------");
        //IPEndPoint ip = myEndPoint as IPEndPoint;
        //Console.WriteLine("Sending request to remote " + ip.Address);
        //byte[] payload = Encoding.ASCII.GetBytes("nice job, man!");
        //socket.SendTo(BuildIcmpMessage(payload, payload.Length), myEndPoint);
        //break;
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();

        options.DontFragment = true;
        string data = "Nice job, man";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        int timeout = 120;
        IPEndPoint ip = myEndPoint as IPEndPoint;
        Console.WriteLine("Sending request to remote " + ip.Address);
        PingReply reply = pingSender.Send(ip.Address, timeout, buffer, options);
        Console.WriteLine(reply.Status);
        break;

    }
}

I tried both commented and uncommented snippets of the code to send my custom response, however nothing of these reached the client (according to wireshark logs from client's machine). The only thing which has reached the client is automatically generated (by .NET socket or something like this) echo reply which has the same payload part as original request. I saw one similar topic here, but there wasn't an answer on the question. If I use Ping class to send response (actually just new ping) then it is failed with Time-out reply status. If I use socket.SendTo then nothing is reached the client. So how can I send my custom reply to client's icmp echo request?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source