private void UpdateRoutes(byte[] response, int bytesReceived) { var neighbor = new RouteEntry() { Ip = ConvertFromBytesToInteger(response, 8), Gateway = ConvertFromIpAddressToInteger(OwnIP), Cost = 0 }; var routes = new List <RouteEntry>(); routes.Add(neighbor); if (bytesReceived > 24) { for (int index = 24; index < bytesReceived; index += 20) { var entry = new RouteEntry() { Ip = ConvertFromBytesToInteger(response, index + 4), Gateway = neighbor.Ip, Cost = ConvertFromBytesToInteger(response, index + 16) + 1 }; //dont add yourself to route list if (ConvertFromIntegerToIpAddress(entry.Ip).Equals(OwnIP)) { continue; } //dont add routes with hop count higher than 15 if (entry.Cost > MaxHopCount) { continue; } routes.Add(entry); } } RouteTable.mutex.WaitOne(); foreach (var entry in routes) { if (RouteTable.Contains(entry)) { int index = RouteTable.IndexOf(entry); var oldEntry = RouteTable[index]; if (oldEntry.Cost >= entry.Cost) { RouteTable[index] = entry; } } else { RouteTable.Add(entry); } } RouteTable.mutex.ReleaseMutex(); }