title: Forbidden socket access attempt Exception when reading from socket from a WebMatrix 3/ASP.NET web page?
body: <p>I have a WebMatrix 3 (same as ASP.NET) web page that opens a socket to a server process running on an Azure hosted Linux VM that listens on a TCP connection for clients.  The Linux VM server process is mine too.  When I run the WebMatrix 3/ASP.NET web site locally from my home PC using a local copy of IIS it works fine (local publish).  When I publish my web site to the web and it is now running on Azure I get the Exception:</p>
  
  <p><strong>An attempt was made to access a socket in a way forbidden by its access permissions</strong></p>
  
  <p>What is really confusing is that the error occurs when I read from the socket but oddly enough not when I connect to it or write to it before-hand.  I know this because the Exception message is adorned with the current operation, and that is set to:</p>
  
  <p><strong>Waiting for and then reading the response from the ChatScript server.</strong></p>
  
  <p>You can see this line in the code below.  Is there something going on with the Azure side that could be blocking reads from the TCP connection to the Linux VM, yet allows connections to that VM and even sends?  I say "even sends" because as you can see from the code below, I immediately send a message to the Linux VM process before I try to read from that connection.</p>
  
  <pre><code>public static string readChatScriptMessage(NetworkStream myNetworkStream)
  {
      if (myNetworkStream == null)
          throw new ArgumentNullException("(readChatScriptMessage) The network stream is unassigned.");
  
      StringBuilder myCompleteMessage = new StringBuilder();
  
      // Check to see if this NetworkStream is readable.
      if (myNetworkStream.CanRead)
      {
          byte[] myReadBuffer = new byte[1024];
          int numberOfBytesRead = 0;
  
          // Incoming message may be larger than the buffer size.
          do
          {
              numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);
  
              myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
  
          }
          while (myNetworkStream.DataAvailable);
  
      }
      else
      {
          if (myNetworkStream == null)
              throw new InvalidOperationException("(readChatScriptMessage) The network stream is unassigned.");
      }
  
      // Print out the received message to the console.
      return myCompleteMessage.ToString();
  } // public static string readChatScriptMessage(NetworkStream myNetworkStream)
  
      // Lookup the IP address for our chatscript server. (Cache this value
      //  in a later build since GetHostEntry() is reportedly a slow call.)
      ipAddress = Dns.GetHostEntry("myazureapp.cloudapp.net").AddressList[0];
  
      strCurrentOperation = "Validating URL arguments (parameters).";
  
      // LoginName, is mandatory.
      strLoginName = checkForValidURLArgument("LoginName", true);
  
      // BotName, is optional.
      strBotName = checkForValidURLArgument("BotName", false);
  
      // User message (chat input), is optional.  But remember,
      //  only send a blank message to start a new session
      //  with ChatScript!  After that, send the user's input
      //  each time.
      strMessage = checkForValidURLArgument("Message", false);
  
      strCurrentOperation = "Connecting to Linux VM TCP server.";
  
      // OK, we're good to go.  We have the 3 URL arguments we were expecting.
  
      // Connect to the ChatScript server.
      tcpCli.Connect(ipAddress, 1024);
  
      strCurrentOperation = "Opening the stream with the server.";
  
      // Open the stream
      streamChatScript = tcpCli.GetStream();
      StreamReader sr = new StreamReader(streamChatScript);
      BinaryWriter sw = new BinaryWriter(streamChatScript);
      // Create a message to send to the server, using the URL argument values
      //  passed to us.
      ChatMessage cm = new ChatMessage(strLoginName, strBotName, strMessage);
  
      strCurrentOperation = "Sending the desired chat message to the server.";
  
      // Send the message to the chat server.
      string strSendChatMsg = cm.ToString();
  
      // Translate the passed message into ASCII and store it as a Byte array.
      Byte[] data = System.Text.Encoding.ASCII.GetBytes(strSendChatMsg); 
  
      for (int i = 0; i &lt; strSendChatMsg.Length; i++)
      {
          data[i] = (byte)strSendChatMsg[i];
      }
  
      // Send the chat message.
      streamChatScript.Write(data, 0, data.Length);
  
      strCurrentOperation = "Waiting for and then reading the response from the server.";
  
      strResponseMsg = ChatMessage.readChatScriptMessage(streamChatScript);
  </code></pre>