title: Why am I getting IOException and SocketException while writing to NetworkStream?
body: <p>I have a dll which connects me to a software, let's call it driver1.
  I have a java program which has to connect to driver1 and query its status.
  The dll provides me functions for connecting and querying the status.</p>
  
  <p>I wrote a c++ program which I gave the dll as a reference and I am able to use the dll from it. I can connect to driver1 and query its status.
  Then I created a TcpListener that my Java program can connect as a client.
  I simply accept the client in c++ and wait for recieving data from Java program.</p>
  
  <p>I send the data from Java side (for example a command representing "connect" method of dll).
  The command is well recieved from the c++ side and it parses and calls the specified function of dll. However when I want to write the return value of the dll function to the NetworkStream to send the return value to Java side I get an exception saying:</p>
  
  <blockquote>
    <p>System.IO.IOException: IOException: Unable to write data to the transport connection. An operation was attempted on something that is not a socket.</p>
  </blockquote>
  
  <p>The c++ side code is the similar to the TcpListener sample from the msdn site:</p>
  
  <pre><code>#using &lt;System.dll&gt;
  
  using namespace System;
  using namespace System::IO;
  using namespace System::Net;
  using namespace System::Net::Sockets;
  using namespace System::Text;
  using namespace System::Threading;
  void main()
  {
      try
      {
  
          // Set the TcpListener on port 1124.
          Int32 port = 1124;
          IPAddress^ localAddr = IPAddress::Parse( "127.0.0.1" );
  
          // TcpListener* server = new TcpListener(port);
          TcpListener^ server = gcnew TcpListener( localAddr,port );
  
          // Start listening for client requests.
          server-&gt;Start();
  
          // Buffer for reading data
          array&lt;Byte&gt;^bytes = gcnew array&lt;Byte&gt;(256);
          String^ data = nullptr;
  
          Console::Write( "Waiting for a connection... " );
  
          // Perform a blocking call to accept requests.
          TcpClient^ client = server-&gt;AcceptTcpClient();
          Console::WriteLine( "Connected!" );
          data = nullptr;
  
          while(true) 
          {
              // Get a stream Object* for reading and writing
              NetworkStream^ stream = client-&gt;GetStream();
              Int32 i;
  
              // Loop to receive all the data sent by the client.
              while ( i = stream-&gt;Read( bytes, 0, bytes-&gt;Length ) )
              {
  
                  // Translate data bytes to a ASCII String*.
                  data = Text::Encoding::ASCII-&gt;GetString( bytes, 0, i );
                  Console::WriteLine( "Received: {0}", data );
  
                  // Process the data sent by the client.
                  /*
                   *
                   * Here I parse the data, understand what method of the dll to call.
                   * Call it and make a String^ from the return value.
                   *
                   *
                   */
                  array&lt;Byte&gt;^msg = Text::Encoding::ASCII-&gt;GetBytes( returnValue );
  
                  // Send back a response.
                  stream-&gt;Write( msg, 0, msg-&gt;Length );
                  Console::WriteLine( "Sent: {0}", data );
              }
          }
  
          // Shutdown and end connection
          client-&gt;Close();
       }
       catch ( SocketException^ e ) 
       {
           Console::WriteLine( "SocketException: {0}", e );
       }
  
       Console::WriteLine( "\nHit enter to continue..." );
       Console::Read();
  }
  </code></pre>
  
  <p>I get the exception on the line where I call:</p>
  
  <blockquote>
    <p>stream->Write()</p>
  </blockquote>
  
  <p>Any ideas about what is happenning?</p>
  
  <p>Thanks in advance...</p>