Socket Binding

I have a Mountaineer ETH Mainboard that I’m programming. When I try to bind a localEP, the program will throw a Socket Exception and shutdown if i plug power in before i plug the ethernet in. I want it to continue trying to Bind to the LocalEP if it fails. I’m not quite sure how to go about coding this. I’m guessing it will have to be in the catch of the try loop but I’m not sure. Has anyone else run into this issue and/or have a solution to the problem? Let me know if you need more info. Thanks!

Not really. That was how my code looked before, but I have changed it significantly since then. I have the bind in a while loop that breaks if if doesn’t send a socket exception when I try to bind. This way it will try to bind until it does successfully bind to the localEP. My hopes is that I could do the bind this way so that If I or someone else plugs in power before the ethernet cord they can don’t have to cycle power off then on again. Right now if I plug in the ethernet late then it remains in the while loop and continuously sends socket exceptions even after the cable has been plugged in.

Here is my current code

            private void StartListening()
            {//listening thread
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
                Thread.Sleep(1500);
                while (true)
                {//wait for network connection
                    try
                    {
                        listener.Bind(localEP);  //binds socket to a local endpoint
                        break;
                    }
                    catch (SocketException exception)
                    {
                        bListening = false;
                        Thread.Sleep(500);
                    }
                }
                listener.Listen(25);     //listens for incoming socket communication
                RunApp.bListening = true;
                while (true)
                {   //creates a socket to accept connections to the local endpoint then returns to listening
                    Socket ClientSocket = listener.Accept();    
                    ProcessClientRequest(ClientSocket, false);  
                }
            }