--- /home/cpdev/src/classpath/gnu/java/net/PlainSocketImpl.java	2005-07-02 21:03:04.000000000 +0000
+++ gnu/java/net/PlainSocketImpl.java	2005-06-30 05:34:09.000000000 +0000
@@ -77,6 +77,23 @@
           System.loadLibrary("javanet");
         }
     }
+  
+  // These fields are mirrored for use in native code to avoid cpp conflicts
+  // when the #defines in system header files are the same as the public fields.
+  static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY,
+                   _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR,
+                   _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR,
+                   _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST,
+                   _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE,
+                   _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF,
+                   _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2,
+                   _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP,
+                   _Jv_IP_TOS_ = SocketOptions.IP_TOS,
+                   _Jv_SO_LINGER_ = SocketOptions.SO_LINGER,
+                   _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT,
+                   _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF,
+                   _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF,
+                   _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE;
 
   /**
    * The OS file handle representing the socket.
@@ -87,6 +104,12 @@
    */
   int native_fd = -1;
 
+  // This value is set/read by setOption/getOption.
+  int timeout = 0;
+  
+  // localAddress cache
+  InetAddress localAddress;
+
   /**
    * A cached copy of the in stream for reading from the socket.
    */
@@ -202,7 +225,7 @@
    *
    * @param stream true for a stream socket, false for a datagram socket
    */
-  protected synchronized native void create(boolean stream) throws IOException;
+  protected native void create(boolean stream) throws IOException;
 
   /**
    * Connects to the remote hostname and port specified as arguments.
@@ -212,7 +235,7 @@
    *
    * @exception IOException If an error occurs
    */
-  protected synchronized void connect(String host, int port) throws IOException
+  protected void connect(String host, int port) throws IOException
   {
     connect(InetAddress.getByName(host), port);
   }
@@ -225,7 +248,10 @@
    *
    * @exception IOException If an error occurs
    */
-  protected native void connect(InetAddress addr, int port) throws IOException;
+  protected void connect(InetAddress host, int port) throws IOException
+  {
+    connect (new InetSocketAddress (host, port), 0);
+  }
 
   /**
    * Connects to the remote socket address with a specified timeout.
@@ -234,33 +260,7 @@
    *
    * @exception IOException If an error occurs
    */
-  protected synchronized void connect(SocketAddress address, int timeout) throws IOException
-  {
-    InetSocketAddress sockAddr = (InetSocketAddress) address;
-    InetAddress addr = sockAddr.getAddress();
-
-    if (addr == null)
-      throw new IllegalArgumentException("address is unresolved: " + sockAddr);
-
-    int port = sockAddr.getPort();
-    
-    if (timeout < 0)
-      throw new IllegalArgumentException("negative timeout");
-
-    Object oldTimeoutObj = null;
-    
-    try
-      {
- 	oldTimeoutObj = this.getOption (SocketOptions.SO_TIMEOUT);
- 	this.setOption (SocketOptions.SO_TIMEOUT, new Integer (timeout));
- 	connect (addr, port);
-      }
-    finally
-      {
-	if (oldTimeoutObj != null)
-	  this.setOption (SocketOptions.SO_TIMEOUT, oldTimeoutObj);
-      }
-  }
+  protected native void connect(SocketAddress addr, int timeout) throws IOException;
 
   /**
    * Binds to the specified port on the specified addr.  Note that this addr
@@ -271,7 +271,7 @@
    *
    * @exception IOException If an error occurs
    */
-  protected synchronized native void bind(InetAddress addr, int port)
+  protected native void bind(InetAddress host, int port)
     throws IOException;
 
   /**
@@ -284,7 +284,7 @@
    * 
    * @exception IOException If an error occurs
    */
-  protected synchronized native void listen(int queuelen)
+  protected native void listen(int queuelen)
     throws IOException;
 
   /**
@@ -293,7 +293,13 @@
    *
    * @param impl The SocketImpl object to accept this connection.
    */
-  protected synchronized native void accept(SocketImpl impl)
+  protected void accept(SocketImpl impl)
+    throws IOException
+  {
+    accept((PlainSocketImpl) impl);
+  }
+
+  private native void accept(PlainSocketImpl impl)
     throws IOException;
 
   /**
@@ -317,32 +323,7 @@
    */
   protected native void close() throws IOException;
 
-  public void sendUrgentData(int data)
-  {
-    throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented");
-  }
-
-  /**
-   * Internal method used by SocketInputStream for reading data from
-   * the connection.  Reads up to len bytes of data into the buffer
-   * buf starting at offset bytes into the buffer.
-   *
-   * @return The actual number of bytes read or -1 if end of stream.
-   *
-   * @exception IOException If an error occurs
-   */
-  protected native int read(byte[] buf, int offset, int len)
-    throws IOException;
-
-  /**
-   * Internal method used by SocketOuputStream for writing data to
-   * the connection.  Writes up to len bytes of data from the buffer
-   * buf starting at offset bytes into the buffer.
-   *
-   * @exception IOException If an error occurs
-   */
-  protected native void write(byte[] buf, int offset, int len)
-    throws IOException;
+  protected native void sendUrgentData(int data) throws IOException;
 
   /**
    * Returns an InputStream object for reading from this socket.  This will
@@ -380,7 +361,7 @@
    * This class contains an implementation of <code>InputStream</code> for 
    * sockets.  It in an internal only class used by <code>PlainSocketImpl</code>.
    *
-   * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
+   * @author Nic Ferrier <nferrier@tapsellferrier.co.uk>
    */
   final class SocketInputStream
     extends InputStream
@@ -410,16 +391,7 @@
      *
      * @exception IOException If an error occurs.
      */
-    public int read() throws IOException
-    {
-      byte buf[] = new byte [1];
-      int bytes_read = read(buf, 0, 1);
- 
-      if (bytes_read == -1)
-        return -1;
-
-      return buf[0] & 0xFF;
-    }
+    public native int read() throws IOException;
 
     /**
      * Reads up to len bytes of data into the caller supplied buffer starting
@@ -433,15 +405,7 @@
      *
      * @exception IOException If an error occurs.
      */
-    public int read (byte[] buf, int offset, int len) throws IOException
-    {
-      int bytes_read = PlainSocketImpl.this.read (buf, offset, len);
-
-      if (bytes_read == 0)
-        return -1;
-
-      return bytes_read;
-    }
+    public native int read(byte[] buf, int offset, int len) throws IOException;
   }
 
   /**
@@ -450,7 +414,7 @@
    * <code>getOutputStream method</code>.  It expects only to  be used in that
    * context.
    *
-   * @author Nic Ferrier (nferrier@tapsellferrier.co.uk)
+   * @author Nic Ferrier  <nferrier@tapsellferrier.co.uk>
    */
   final class SocketOutputStream
     extends OutputStream
@@ -474,11 +438,7 @@
      *
      * @exception IOException If an error occurs
      */
-    public void write(int b) throws IOException
-    {
-      byte buf[] = { (byte) b };
-      write(buf, 0, 1);
-    }
+    public native void write(int b) throws IOException;
 
     /**
      * Writes len number of bytes from the array buf to the stream starting
@@ -490,9 +450,6 @@
      *
      * @exception IOException If an error occurs.
      */
-    public void write (byte[] buf, int offset, int len) throws IOException
-    {
-      PlainSocketImpl.this.write (buf, offset, len);
-    }
+    public native void write(byte[] buf, int offset, int len) throws IOException;
   }
 }
