As Dave says: your code is waiting for responses, and that can't be speed up.
However, it might be worth using the Stopwatch class to work out exactly where it is going slowly:
void ScanForServer()
{
IPEndPoint ReceiveIP = null;
UdpClient listener = null;
Byte[] RequestData = null;
byte[] BytesReceived = null;
int UDPPort = 3483;
Stopwatch s1 = new Stopwatch();
Stopwatch s2 = new Stopwatch();
Stopwatch s3 = new Stopwatch();
Stopwatch s4 = new Stopwatch();
Stopwatch s5 = new Stopwatch();
Stopwatch s6 = new Stopwatch();
try
{
s1.Start();
ReceiveIP = new IPEndPoint(IPAddress.Any, UDPPort);
s1.Stop();
s2.Start();
listener = new UdpClient(this.UDPPort);
s2.Stop();
s3.Start();
RequestData = listener.Receive(ref ReceiveIP);
s3.Stop();
s4.Start();
listener.Send(RequestData, RequestData.Length, ReceiveIP);
s4.Stop();
s5.Start();
BytesReceived = listener.Receive(ref ReceiveIP);
s5.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
s6.Start();
if (listener != null && listener.Client.Connected)
listener.Close();
s6.Stop();
}
Console.WriteLine("{0}:{1}:{2}:{3}:{4}:{5}",
s1.ElapsedMilliseconds,
s2.ElapsedMilliseconds,
s3.ElapsedMilliseconds,
s4.ElapsedMilliseconds,
s5.ElapsedMilliseconds,
s6.ElapsedMilliseconds);
}
There probably isn't anything you can do about it in your code, but it may help you to know which part is sluggish.