gt:       if (addresses == null || addresses.length < 1) {
context: /*
   * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
   *
   * This file is part of Resin(R) Open Source
   *
   * Each copy or derived work must preserve the copyright notice and this
   * notice unmodified.
   *
   * Resin Open Source is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * Resin Open Source is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
   * of NON-INFRINGEMENT.  See the GNU General Public License for more
   * details.
   *
   * You should have received a copy of the GNU General Public License
   * along with Resin Open Source; if not, write to the
   *
   *   Free Software Foundation, Inc.
   *   59 Temple Place, Suite 330
   *   Boston, MA 02111-1307  USA
   *
   * @author Scott Ferguson
   */
  
  package com.caucho.quercus.lib.file;
  
  import com.caucho.quercus.annotation.NotNull;
  import com.caucho.quercus.annotation.Optional;
  import com.caucho.quercus.annotation.ReturnNullAsFalse;
  import com.caucho.quercus.env.BooleanValue;
  import com.caucho.quercus.env.Env;
  import com.caucho.quercus.env.LongValue;
  import com.caucho.quercus.env.StringValue;
  import com.caucho.quercus.env.Value;
  import com.caucho.quercus.module.AbstractQuercusModule;
  import com.caucho.util.L10N;
  import com.caucho.vfs.TempBuffer;
  import com.caucho.vfs.TempCharBuffer;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.InetSocketAddress;
  import java.net.Socket;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   * Information and actions for about sockets
   */
  public class SocketModule extends AbstractQuercusModule {
    private static final L10N L = new L10N(SocketModule.class);
    private static final Logger log
      = Logger.getLogger(SocketModule.class.getName());
  
    private static final int AF_UNIX = 1;
    private static final int AF_INET = 2;
    private static final int AF_INET6 = 10;
    private static final int SOCK_STREAM = 1;
    private static final int SOCK_DGRAM = 2;
    private static final int SOCK_RAW = 3;
    private static final int SOCK_SEQPACKET = 5;
    private static final int SOCK_RDM = 4;
    private static final int MSG_OOB = 1;
    private static final int MSG_WAITALL = 256;
    private static final int MSG_PEEK = 2;
    private static final int MSG_DONTROUTE = 4;
    private static final int SO_DEBUG = 1;
    private static final int SO_REUSEADDR = 2;
    private static final int SO_KEEPALIVE = 9;
    private static final int SO_DONTROUTE = 5;
    private static final int SO_LINGER = 13;
    private static final int SO_BROADCAST = 6;
    private static final int SO_OOBINLINE = 10;
    private static final int SO_SNDBUF = 7;
    private static final int SO_RCVBUF = 8;
    private static final int SO_SNDLOWAT = 19;
    private static final int SO_RCVLOWAT = 18;
    private static final int SO_SNDTIMEO = 21;
    private static final int SO_RCVTIMEO = 20;
    private static final int SO_TYPE = 3;
    private static final int SO_ERROR = 4;
    private static final int SOL_SOCKET = 1;
    private static final int SOMAXCONN = 128;
    private static final int PHP_NORMAL_READ = 1;
    private static final int PHP_BINARY_READ = 2;
    private static final int SOCKET_EPERM = 1;
    private static final int SOCKET_ENOENT = 2;
    private static final int SOCKET_EINTR = 4;
    private static final int SOCKET_EIO = 5;
    private static final int SOCKET_ENXIO = 6;
    private static final int SOCKET_E2BIG = 7;
    private static final int SOCKET_EBADF = 9;
    private static final int SOCKET_EAGAIN = 11;
    private static final int SOCKET_ENOMEM = 12;
    private static final int SOCKET_EACCES = 13;
    private static final int SOCKET_EFAULT = 14;
    private static final int SOCKET_ENOTBLK = 15;
    private static final int SOCKET_EBUSY = 16;
    private static final int SOCKET_EEXIST = 17;
    private static final int SOCKET_EXDEV = 18;
    private static final int SOCKET_ENODEV = 19;
    private static final int SOCKET_ENOTDIR = 20;
    private static final int SOCKET_EISDIR = 21;
    private static final int SOCKET_EINVAL = 22;
    private static final int SOCKET_ENFILE = 23;
    private static final int SOCKET_EMFILE = 24;
    private static final int SOCKET_ENOTTY = 25;
    private static final int SOCKET_ENOSPC = 28;
    private static final int SOCKET_ESPIPE = 29;
    private static final int SOCKET_EROFS = 30;
    private static final int SOCKET_EMLINK = 31;
    private static final int SOCKET_EPIPE = 32;
    private static final int SOCKET_ENAMETOOLONG = 36;
    private static final int SOCKET_ENOLCK = 37;
    private static final int SOCKET_ENOSYS = 38;
    private static final int SOCKET_ENOTEMPTY = 39;
    private static final int SOCKET_ELOOP = 40;
    private static final int SOCKET_EWOULDBLOCK = 11;
    private static final int SOCKET_ENOMSG = 42;
    private static final int SOCKET_EIDRM = 43;
    private static final int SOCKET_ECHRNG = 44;
    private static final int SOCKET_EL2NSYNC = 45;
    private static final int SOCKET_EL3HLT = 46;
    private static final int SOCKET_EL3RST = 47;
    private static final int SOCKET_ELNRNG = 48;
    private static final int SOCKET_EUNATCH = 49;
    private static final int SOCKET_ENOCSI = 50;
    private static final int SOCKET_EL2HLT = 51;
    private static final int SOCKET_EBADE = 52;
    private static final int SOCKET_EBADR = 53;
    private static final int SOCKET_EXFULL = 54;
    private static final int SOCKET_ENOANO = 55;
    private static final int SOCKET_EBADRQC = 56;
    private static final int SOCKET_EBADSLT = 57;
    private static final int SOCKET_ENOSTR = 60;
    private static final int SOCKET_ENODATA = 61;
    private static final int SOCKET_ETIME = 62;
    private static final int SOCKET_ENOSR = 63;
    private static final int SOCKET_ENONET = 64;
    private static final int SOCKET_EREMOTE = 66;
    private static final int SOCKET_ENOLINK = 67;
    private static final int SOCKET_EADV = 68;
    private static final int SOCKET_ESRMNT = 69;
    private static final int SOCKET_ECOMM = 70;
    private static final int SOCKET_EPROTO = 71;
    private static final int SOCKET_EMULTIHOP = 72;
    private static final int SOCKET_EBADMSG = 74;
    private static final int SOCKET_ENOTUNIQ = 76;
    private static final int SOCKET_EBADFD = 77;
    private static final int SOCKET_EREMCHG = 78;
    private static final int SOCKET_ERESTART = 85;
    private static final int SOCKET_ESTRPIPE = 86;
    private static final int SOCKET_EUSERS = 87;
    private static final int SOCKET_ENOTSOCK = 88;
    private static final int SOCKET_EDESTADDRREQ = 89;
    private static final int SOCKET_EMSGSIZE = 90;
    private static final int SOCKET_EPROTOTYPE = 91;
    private static final int SOCKET_ENOPROTOOPT = 92;
    private static final int SOCKET_EPROTONOSUPPORT = 93;
    private static final int SOCKET_ESOCKTNOSUPPORT = 94;
    private static final int SOCKET_EOPNOTSUPP = 95;
    private static final int SOCKET_EPFNOSUPPORT = 96;
    private static final int SOCKET_EAFNOSUPPORT = 97;
    private static final int SOCKET_EADDRINUSE = 98;
    private static final int SOCKET_EADDRNOTAVAIL = 99;
    private static final int SOCKET_ENETDOWN = 100;
    private static final int SOCKET_ENETUNREACH = 101;
    private static final int SOCKET_ENETRESET = 102;
    private static final int SOCKET_ECONNABORTED = 103;
    private static final int SOCKET_ECONNRESET = 104;
    private static final int SOCKET_ENOBUFS = 105;
    private static final int SOCKET_EISCONN = 106;
    private static final int SOCKET_ENOTCONN = 107;
    private static final int SOCKET_ESHUTDOWN = 108;
    private static final int SOCKET_ETOOMANYREFS = 109;
    private static final int SOCKET_ETIMEDOUT = 110;
    private static final int SOCKET_ECONNREFUSED = 111;
    private static final int SOCKET_EHOSTDOWN = 112;
    private static final int SOCKET_EHOSTUNREACH = 113;
    private static final int SOCKET_EALREADY = 114;
    private static final int SOCKET_EINPROGRESS = 115;
    private static final int SOCKET_EISNAM = 120;
    private static final int SOCKET_EREMOTEIO = 121;
    private static final int SOCKET_EDQUOT = 122;
    private static final int SOCKET_ENOMEDIUM = 123;
    private static final int SOCKET_EMEDIUMTYPE = 124;
    private static final int SOL_TCP = 6;
    private static final int SOL_UDP = 17;
  
    private static final HashMap<String,Value> _constMap
      = new HashMap<String,Value>();
  
    /**
     * Returns the constants defined by this module.
     */
    public Map<String,Value> getConstMap()
    {
      return _constMap;
    }
   
    @ReturnNullAsFalse
    public static SocketInputOutput socket_create(Env env,
                                                  int domain, 
                                                  int type, 
                                                  int protocol)
    {
      try {
        SocketInputOutput.Domain socketDomain = SocketInputOutput.Domain.AF_INET;
  
        switch (domain) {
          case AF_INET:
            socketDomain = SocketInputOutput.Domain.AF_INET;
            break;
          case AF_INET6:
            socketDomain = SocketInputOutput.Domain.AF_INET6;
            break;
          case AF_UNIX:
            env.warning(L.l("Unix sockets not supported"));
            return null;
          default:
            env.warning(L.l("Unknown domain: {0}", domain));
            return null;
        }
  
        switch (type) {
          case SOCK_STREAM:
            return new TcpInputOutput(env, new Socket(), socketDomain);
          case SOCK_DGRAM:
            return new UdpInputOutput(env, new DatagramSocket(), socketDomain);
          default:
            env.warning(L.l("socket stream not socked"));
            return null;
        }
  
      } catch (Exception e) {
        env.warning(e);
        return null;
      }
    }
  
    public static boolean socket_bind(Env env,
                                      @NotNull SocketInputOutput socket,
                                      StringValue address, 
                                      @Optional("0") int port)
    {
      try {
        InetAddress []addresses = InetAddress.getAllByName(address.toString());
  
        if (addresses == null || addresses.length < 1) {
          //XXX: socket.setError();
          return false;
        }
  
        InetSocketAddress socketAddress = 
          new InetSocketAddress(addresses[0], port);
  
        socket.bind(socketAddress);
  
        return true;
      } catch (IOException e) {
        return false;
      }
    }
  
    public static void socket_close(Env env, @NotNull SocketInputOutput socket)
    {
      socket.close();
    }
  
    public static boolean socket_connect(Env env,
                                         @NotNull SocketInputOutput socket,
                                         StringValue address, @Optional int port)
    {
      try {
        InetAddress []addresses = InetAddress.getAllByName(address.toString());
  
  