Socket exception - WiFi issue

Hey, guys, I am struggling to use the wifi_rs21 module properly.

I succeeded in using the wifi module to connect to the network and post data to a web service at certain interval. But the socket exception would pop out after the application successfully running for a while. From the exception, according to socket error code description given by Microsoft, the reason might be no buffer space available. How can I solve this problem?

Another thing is after the WiFi_RS21 module connected to the wireless network, NetworkUp event was never triggered.

Here is the description of the exception:

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::socket [IP: 0000] ####
#### System.Net.Sockets.Socket::.ctor [IP: 001f] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 0132] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetRequestStream [IP: 0008] ####
#### GadgeteerLibrary.RESTClient::SendHistoricData [IP: 0038] ####
#### GadgeteerUseCaseOne.Program::sendTimer_Tick [IP: 0054] ####
#### Gadgeteer.Timer::dt_Tick [IP: 0018] ####
#### Microsoft.SPOT.DispatcherTimer::FireTick [IP: 0010] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 0054] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001a] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Gadgeteer.Program::Run [IP: 0020] ####
#### SocketException ErrorCode = 10055
#### SocketException ErrorCode = 10055

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10055
#### SocketException ErrorCode = 10055
#### SocketException ErrorCode = 10055
#### SocketException ErrorCode = 10055
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in System.Http.dll
#### SocketException ErrorCode = 10055
#### SocketException ErrorCode = 10055

Here is part of my code showing how to connect to the network:


        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
            this.InitializeNetwork();
            this.InitializeSDCard();
            this.InitializeSensor();
        }

        void InitializeNetwork()
        {
            if (!wifi_RS21.Interface.IsOpen)
                wifi_RS21.Interface.Open();

            if (!wifi_RS21.Interface.NetworkInterface.IsDhcpEnabled)
                wifi_RS21.Interface.NetworkInterface.EnableDhcp();

            NetworkInterfaceExtension.AssignNetworkingStackTo(wifi_RS21.Interface);

            // use the router's DHCP server to set my network info
            wifi_RS21.UseDHCP();

            // setup events
            wifi_RS21.NetworkDown += wifi_NetworkDown;
            wifi_RS21.NetworkUp += wifi_NetworkUp;
            wifi_RS21.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;
            wifi_RS21.Interface.WirelessConnectivityChanged += Interface_WirelessConnectivityChanged;

            try
            {
                this.WifiConnect();

                if (wifi_RS21.IsNetworkConnected)
                {
                    // after connecting, set up local time and register the sensors.
                    this.SetLocalTime();
                    this.RegisterSensor();
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
        }

        void WifiConnect()
        {
            // to use hold the network to connect to
            WiFiNetworkInfo[] targetNetwork = wifi_RS21.Interface.Scan(targetSSID);

            // connect to the specified network
            if (targetNetwork != null && targetNetwork.Length > 0)
            {
                WiFiNetworkInfo target = targetNetwork[0];
                wifi_RS21.Interface.Join(target, password);

                Debug.Print("Network joined");
                Debug.Print("Up: " + wifi_RS21.IsNetworkUp.ToString());
                Debug.Print("IP Address: " + wifi_RS21.NetworkSettings.IPAddress);
            }
            else
                Debug.Print(targetSSID + " wireless network not found");
        }

Thank you!

@ gzsimonfbi - Are you closing your sessions after you use them?

@ Mike - Hi, I am using the network to send HTTP request to post data to a REST Web service.

How do I close the session?

This is the code about POST request:


        public bool RegisterSensor()
        {
            string jsonData = RegistrationJSON();
            Debug.Print(jsonData);

            try
            {
                HttpWebRequest request
                    = CreateRequest("Services/device.svc/device");
                request.Method = "POST";
                request.ContentLength = jsonData.Length;

                HttpWebResponse response = GetResponse(request, jsonData);

                foreach (Sensor s in sensors)
                {
                    s.Id = response.GetResponseHeader(s.Name);
                }

                using (StreamReader streamReader =
                    new StreamReader(response.GetResponseStream()))
                {
                    if ((response.StatusCode == HttpStatusCode.OK) 
                        && streamReader.ReadToEnd().Equals("\"Success\""))
                        return true;
                    else
                        return false;
                }
            }
            catch (WebException we)
            {
                Debug.Print(we.Message);
                return false;
            }
        }

        private HttpWebRequest CreateRequest(string suffixURI)
        {
            Uri serviceEndPoint = new Uri(baseURI.AbsoluteUri + suffixURI); 
            HttpWebRequest request =
                (HttpWebRequest)WebRequest.Create(serviceEndPoint);

            // Set APIKey as the Authorization header field to use for this request.
            request.Headers.Add("Authorization", "APIKey " + this.apiKey);
            request.ContentType = "application/json; charset=utf-8";
            return request;
        }

        private HttpWebResponse GetResponse(HttpWebRequest request, string jsonData)
        {
            using (StreamWriter requestWriter =
                new StreamWriter(request.GetRequestStream()))
            {
                requestWriter.Write(jsonData);
            }

            return (HttpWebResponse)request.GetResponse();
        }