public static void countWithStateful() throws NamingException, InterruptedException {
		
		int i, lostTimes = 0;
		long threadId = Thread.currentThread().getId();
		boolean lost = false;
		RemoteSFSB sfb = lookupRemoteSFSB();
		
		System.out.println(threadId + ": Obtained a remote stateful bean for invocation.");
		long time = System.nanoTime();

        
		sfb.setCounter(0l);
		for (i = 1; i <= Variables.repeat; i++) {
			try {
				if (Variables.verbose) {
					System.out.println(threadId + ": " + sfb.incrementAndGetCounter());
				} else {
					sfb.incrementAndGetCounter();
				}
			} catch (Exception ex) {
				System.out.println("Exception " + ex.getClass().getCanonicalName() + " was thrown. Stateful bean is lost.");
				lost = true;
				lostTimes++;
			}
			
			while (lost) {
				if (Variables.verbose) {
					System.out.println("Attempting creation of new stateful session.");
				}
				try {
					sfb = lookupRemoteSFSB();
				} catch (Exception ex) {
					System.out.println("Exception " + ex.getClass().getCanonicalName() + " was thrown. Stateful bean could not be created.");
					continue;
				}
				lost = false;
				Thread.sleep(100);
			}
			Thread.sleep(Variables.sleep);
		}
		
		time = System.nanoTime()  - time;
		System.out.println(threadId + ": Number of stateful invocations: " + (i - 1) + ", elapsed time: " + ((double) time / 1000000000.0) + " seconds. Beans were lost " + lostTimes + " times.");
		
	}