title: How to broadcast WOL over ethernet without enabling IP directed broadcast
body: <p>I am building a Wake On Lan program in C#  i have found lots of code for WOL but whatever i have found thus far does not work. i believe this is because i cannot enable IP directed broadcast (The customer's security policy will not enable this in order to prevent DOS attacks).</p>
  
  <p><strong>i am looking for a way to send the magic packet over ethernet directly to the requested mac address</strong> - right now it seems to be sending over UDP to 255.255.255.255</p>
  
  <p>(What i am don't understand is why it needs to send to 255.255.255.255 and not to the mac itself)</p>
  
  <p>here is the code i have as of now (can't remember where i found it).</p>
  
  <pre><code>public static bool WakeOnLan(string MacAddress)
  {
      try
      {
          MacAddress = MacAddress.Replace("-", "");
          MacAddress = MacAddress.Replace(":", "");
          if (MacAddress.Length != 12)
          {
              return false;
          }
          byte[] mac = new byte[6];
          for (int k = 0; k &lt; 6; k++)
          {
              mac[k] = Byte.Parse(MacAddress.Substring(k * 2, 2), System.Globalization.NumberStyles.HexNumber);
          }
  
          // WOL packet is sent over UDP 255.255.255.0:40000.
          System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();
          client.Connect(System.Net.IPAddress.Broadcast, 4000);
  
          byte[] packet = new byte[17 * 6];
  
          for (int i = 0; i &lt; 6; i++)
              packet[i] = 0xFF;
  
          for (int i = 1; i &lt;= 16; i++)
              for (int j = 0; j &lt; 6; j++)
                  packet[i * 6 + j] = mac[j];
  
          client.Send(packet, packet.Length);
          return true;
      }
      catch
      {
          return false;
      }
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>thanx</p>