# core/implementation/RemoteObject.tcl

    @implementation RemoteObject {

      + newWithHost:andCommand: { aHost aCommand } {
          return [$self newWithConnection: [[Connection new] host: $aHost] \
                               andCommand: $aCommand]
        }

      + newWithHost:port:andCommand: { aHost aPort aCommand } {
          return [$self newWithConnection: \
                  [[[Connection new] host: $aHost] \
                                     port: $aPort] \
                               andCommand: $aCommand]
        }

      + newWithConnection:andCommand: { aConnection aCommand } {
          #- make sure the connection is opened
          if {![$aConnection isAlive]} {
            if [catch {$aConnection open} err] {
              return -code error $err
            }
          }
          set remoteId [$aConnection remoteInvocation: $aCommand]

          if {![string length $remoteId] || ![regexp id* $remoteId]} {
            return -code error "couldn't create remote object ! \
              (got \"$remoteId\" from server)"
          }

          set id [@newInstanceFromClass $self]
          upvar #0 @$id ID
          set ID(connection) $aConnection
          set ID(remoteId) $remoteId
          return $id
        }

      - forwardInvocation: { { anInvocation {} } } {
          #- make sure the connection is opened
          if {![$connection isAlive] && ![$connection open]} {
            return -code error "couldn't open connection \
              with [$connection host] at port [$connection port]"
          }
          set remoteInvocation "$remoteId "
          append remoteInvocation $anInvocation
          return [$connection remoteInvocation: $remoteInvocation]
        }

      - release {} {
          return [$self dealloc]
        }

      - dealloc {} {
          if [$connection isAlive] {
            $connection remoteInvocation: "$remoteId dealloc"
          }
          $connection release

          upvar #0 @$self SELF
          rename $self {}
          unset SELF
          return
        }
    }