using System;
using System.Net.Sockets;
using System.IO;

namespace bigloo
{
  public class output_socket_port: output_port
  {
    public Socket _socket;

    public output_socket_port( Socket  _socket )
    {
      this._socket= _socket;
    }

    public override Object close()
    {
      _socket= null;
      return this;
    }

    public override Object flush()
    {
      // Nothing to flush...
      return bbool.vrai;
    }

    public override void write( int  cn ) 
    {
      try
      {
        byte[]     buffer= { (byte)cn };

        _socket.Send( buffer );
      }
      catch (Exception e)
      {
        if (_socket != null)
          foreign.fail( "write", e.Message, this );
      }
    }

    public override void write( byte[]  s ) 
    {
      try
      {
        // !!!!! CARE pnet bug
        int        n= s.Length;

        if (n == 3294)
          for ( int i= 0 ; i < 3294 ; ++i )
            write( s[i] );
        else
          _socket.Send( s );
      }
      catch (Exception e)
      {
        if (_socket != null)
          foreign.fail( "write", e.Message, this );
      }
    }

    public override void write( String  s )
    {
      try
      {
        byte[]     buffer= foreign.getbytes( s );

        _socket.Send( buffer );
      }
      catch (Exception e)
      {
        if (_socket != null)
          foreign.fail( "write", e.Message, this );
      }
    }
  }
}
